added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBASPNETShareSessionBetweenSubDomains / VBASPNETShareSessionBetweenSubDomainsModule / SharedSessionModule.vb
blobd8e9697e02be93c1b13071801cd5028fd730f810
1 '****************************** Module Header ******************************\
2 ' Module Name: SharedSessionModule.vb
3 ' Project: VBASPNETShareSessionBetweenSubDomains
4 ' Copyright (c) Microsoft Corporation
6 ' This project demonstrates how to configure a SQL Server as SessionState and
7 ' make a module to share Session between two Web Sites with the same root domain.
8 '
9 ' SharedSessionModule is used to make Web Sites use the same Application Id and
10 ' Session Id to achieve sharing Session.
12 ' This source is subject to the Microsoft Public License.
13 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 ' All other rights reserved.
16 '*****************************************************************************/
18 Imports System.Web
19 Imports System.Configuration
20 Imports System.Reflection
22 ''' <summary>
23 ''' A HttpModule used for sharing the session between Applications in
24 ''' sub domains.
25 ''' </summary>
26 Public Class SharedSessionModule
27 Implements IHttpModule
28 ' Cache settings on memory.
29 Protected Shared applicationName As String = ConfigurationManager.AppSettings("ApplicationName")
30 Protected Shared rootDomain As String = ConfigurationManager.AppSettings("RootDomain")
32 #Region "IHttpModule Members"
33 ''' <summary>
34 ''' Initializes a module and prepares it to handle requests.
35 ''' </summary>
36 ''' <param name="context">
37 ''' An System.Web.HttpApplication
38 ''' that provides access to the methods,
39 ''' properties, and events common to all application objects within
40 ''' an ASP.NET application.
41 ''' </param>
42 Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
43 ' This module requires both Application Name and Root Domain to work.
44 If String.IsNullOrEmpty(applicationName) OrElse String.IsNullOrEmpty(rootDomain) Then
45 Return
46 End If
48 ' Change the Application Name in runtime.
49 Dim runtimeInfo As FieldInfo = GetType(HttpRuntime).GetField("_theRuntime", BindingFlags.[Static] Or BindingFlags.NonPublic)
50 Dim theRuntime As HttpRuntime = DirectCast(runtimeInfo.GetValue(Nothing), HttpRuntime)
51 Dim appNameInfo As FieldInfo = GetType(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance Or BindingFlags.NonPublic)
53 appNameInfo.SetValue(theRuntime, applicationName)
55 ' Subscribe Events.
56 AddHandler context.PostRequestHandlerExecute, AddressOf context_PostRequestHandlerExecute
57 End Sub
59 ''' <summary>
60 ''' Disposes of the resources (other than memory) used by the module
61 ''' that implements.
62 ''' </summary>
63 Public Sub Dispose() Implements IHttpModule.Dispose
64 End Sub
65 #End Region
67 ''' <summary>
68 ''' Before sending response content to client, change the Cookie to Root Domain
69 ''' and store current Session Id.
70 ''' </summary>
71 ''' <param name="sender">
72 ''' An instance of System.Web.HttpApplication that provides access to
73 ''' the methods, properties, and events common to all application
74 ''' objects within an ASP.NET application.
75 ''' </param>
76 Private Sub context_PostRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
77 Dim context As HttpApplication = DirectCast(sender, HttpApplication)
79 ' ASP.NET store a Session Id in cookie to specify current Session.
80 Dim cookie As HttpCookie = context.Response.Cookies("ASP.NET_SessionId")
82 If context.Session IsNot Nothing AndAlso Not String.IsNullOrEmpty(context.Session.SessionID) Then
83 ' Need to store current Session Id during every request.
84 cookie.Value = context.Session.SessionID
86 ' All Applications use one root domain to store this Cookie
87 ' So that it can be shared.
88 If rootDomain <> "localhost" Then
89 cookie.Domain = rootDomain
90 End If
92 ' All Virtual Applications and Folders share this Cookie too.
93 cookie.Path = "/"
94 End If
95 End Sub
96 End Class